#!/usr/bin/env bash
# Summary: Display prefix for a PHP version
# Usage: phpenv prefix [<version>]
#
# Displays the directory where a PHP version is installed. If no
# version is given, `phpenv prefix' displays the location of the
# currently selected version.

set -e
[ -n "$PHPENV_DEBUG" ] && set -x

# Provide phpenv completions
if [ "$1" = "--complete" ]; then
  echo system
  exec phpenv-versions --bare
fi

if [ -n "$1" ]; then
  export PHPENV_VERSION="$1"
elif [ -z "$PHPENV_VERSION" ]; then
  PHPENV_VERSION="$(phpenv-version-name)"
fi

if [ "$PHPENV_VERSION" = "system" ]; then
  PHP_PATH="$(phpenv-which php)"
  PHP_PATH="${PHP_PATH%/*}"
  echo "${PHP_PATH%/bin}"
  exit
fi

PHPENV_PREFIX_PATH="${PHPENV_ROOT}/versions/${PHPENV_VERSION}"
if [ ! -d "$PHPENV_PREFIX_PATH" ]; then
  echo "phpenv: version \`${PHPENV_VERSION}' not installed" >&2
  exit 1
fi

echo "$PHPENV_PREFIX_PATH"
